<?php
namespace app\index\controller;
use think\facade\Env;
use \PHPQRCode\QRcode;
use app\common\model\PHPQrcode;
use think\Request;

class Index
{
    public function index(Request $request)
    {

        // return redirect('admin/Index/index');
//        $url = 'http://baidu.com';
//        $logo = 'https://qzapp.qlogo.cn/qzapp/101649470/74E89DBEECE5A203AF1B840DBE25C906/30';
//        $logo = Env::get('root_path').'public/static/logo.png'; //你的LOGO图片
//        $background = Env::get('root_path').'public/shengji.jpg'; //你的LOGO图片
        $text = $request -> param('text');
        $type = $request -> param('type',1);
        $length = mb_strlen($text,'UTF-8');
        if($type==2){
            $bgHeight = (int)$length * 30+5;
            $bgWidth = 30;
            $fontLeft = 5;
            $fontTop = 0;
            $fontWidth = 35;
            $spacing = 0;
        }else{
            $bgWidth = (int)$length * 25;
            $bgHeight = 30;
            $fontLeft = 5;
            $fontTop = 21;
            $fontWidth = 300;
            $spacing = 5;
        }
        //创建画布
        $QRCode = new PHPQrcode();
        $canvas = $QRCode->createCanvas($bgWidth,$bgHeight, array(
            0,
            0,
            0
        ));
//        $saveName = 'uploads/qrcode/'.md5(microtime(true)).'.png';
//        imagepng($canvas,$saveName);
//        imagedestroy($canvas);
//        $QRCode->drawImage($canvas,$background,0,0,600,1000);
//        //画二维码
//        $QRCode->drawQrcode($canvas,$url,200,750,4,'H',2,200,200);
        //画文本
        $QRCode->drawText($canvas,$text,13,array(0,0,0),$fontLeft,$fontTop,0,$fontWidth,$spacing);
        //画头像
//        $QRCode->drawImage($canvas,$logo,270,820,60,60);
        $saveName = 'uploads/qrcode/'.md5(microtime(true)).'.png';
//        $saveName = 'erweima.png';
        $QRCode->outputQrcode($canvas,$saveName);
    }

    public function ceshi(){
        $image = imagecreatetruecolor(100, 100);

// Transparent Background
        imagealphablending($image, false);
        $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
        imagefill($image, 0, 0, $transparency);
        imagesavealpha($image, true);

// Drawing over
//        $black = imagecolorallocate($image, 0, 0, 0);
//        imagefilledrectangle($image, 25, 25, 75, 75, $black);
        $saveName = 'uploads/qrcode/'.md5(microtime(true)).'.png';
        header('Content-Type: image/png');
        imagepng($image,$saveName);
        imagedestroy($image);
    }
}
